package com.wuad.platformwuad.util;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * 地图-位置与坐标转换
 *
 * @author ：Wuad
 * @date ：Created in 2021/4/8 15:29
 * @description :
 */
public class AddressLngLatExchange {

    public String getLngLat(String address) {
        StringBuffer json = new StringBuffer();
        try {
            URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=3e591ed40dc28a46fb03c06a638deeac");
            URLConnection yc = u.openConnection();
            //读取返回的数据
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));
            String inputline = null;
            while((inputline=in.readLine())!=null){
                json.append(inputline);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String jsonStr=json.toString();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);

        //判断输入的位置点是否存在
        if(jsonObject.getJSONArray("geocodes").size()>0) {
            return jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString();
        }else {
            return null;
        }
    }
    public static void main(String[] args)  {
        AddressLngLatExchange addressLngLatExchange=new AddressLngLatExchange();
        String address = "潍坊市高新区中动大厦";
        String [] str = addressLngLatExchange.getLngLat(address).split(",");
        System.out.println("xxxx" + str[0]);
        System.out.println("yyyy" + str[1]);
    }
}
